package com.lqm.web.controller.home;

import com.lqm.entity.admin.AdminUser;
import com.lqm.entity.home.HomeUser;
import com.lqm.service.home.HomeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-07 12:30
 */

@RestController
@RequestMapping("/home")
public class HomeUserController {

    @Autowired
    HomeUserService homeUserService;

    /**
     * 获取所有用户
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getAllHomeUser")
    public Map<String, Object> getAllHomeUser(int page, int limit) {
        HashMap<String, Object> map = new HashMap<>(0);
        List<HomeUser> all = homeUserService.findAll(page, limit);
        for (HomeUser user : all) {
            user.setUserAddresses(null);
            user.setProjects(null);
        }
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", homeUserService.getCount());
        map.put("data", all);
        return map;
    }


    /**
     * 登录接口
     *
     * @param username 用户名
     * @param password 密码
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public Map<String, Object> login(String username, String password, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser user = new HomeUser();

        user.setUsername(username);
        user.setPassword(password);
        HomeUser homeUser = homeUserService.login(user);
        if (homeUser != null) {
            System.out.println(homeUser);
            homeUser.setProjects(null);
            homeUser.setUserAddresses(null);
            homeUser.setPassword(null);
            session.setAttribute("home_user", homeUser);
            map.put("code", 0);
            map.put("msg", "登录成功");
            String targetUrl = (String) session.getAttribute("targetUrl");
            if (targetUrl!=null){
                map.put("targetUrl",targetUrl);
                session.setAttribute("targetUrl",null);
            }else {
                map.put("targetUrl","/index.html");
            }

            map.put("data", homeUser);
        } else {
            map.put("code", 101);
            map.put("msg", "用户名和密码不匹配");
        }

        return map;
    }


    /**
     * 注册接口
     *
     * @param homeUser 注册信息
     * @param session  保存session
     * @return
     */
    @RequestMapping("/sign")
    public Map<String, Object> sign(HomeUser homeUser, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        //System.out.println(homeUser);
        //System.out.println("--------------sign----------------------");
        if (homeUser != null && !"".equals(homeUser.getUsername()) && !homeUser.getUsername().equals(null) && !"".equals(homeUser.getPassword()) && !homeUser.getPassword().equals(null)) {
            Date date = new Date(System.currentTimeMillis());
            homeUser.setCreateTime(new Timestamp(date.getTime()));

            HomeUser sign = homeUserService.sign(homeUser);
            if (sign != null) {
                map.put("code", 0);
                map.put("msg", "注册成功");
                //保存session
                session.setAttribute("home_user", homeUser);
            } else {
                map.put("code", 101);
                map.put("msg", "用户名已经存在");
            }


        } else {
            map.put("code", 101);
            map.put("msg", "注册失败");
        }
        return map;
    }

    /**
     * 更新用户
     *
     * @param homeUser
     * @return
     */
    @RequestMapping("updateUser")
    public Map<String, Object> updateUser(HomeUser homeUser, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        if (session.getAttribute("home_user") != null) {
            System.out.println(homeUser);
            try {
                HomeUser save = homeUserService.save(homeUser);

                session.setAttribute("home_user", save);
//                /upload/2018-12-15/8b0a2cd3294d4165a3f26a35af85c66bcart-error.png
                System.out.println(session.getAttribute("home_user"));
                map.put("msg", "更新成功");
                map.put("code", 0);
            } catch (Exception e) {

                throw new RuntimeException();
            }
        } else {
            map.put("msg", "更新失败，没有登录");
            map.put("code", 1);
        }


        return map;
    }

    /**
     * 获取当前用户信息
     */
    @ModelAttribute
    public void getUser(HttpServletRequest request, Model model) {
        HomeUser user = (HomeUser) request.getSession().getAttribute("home_user");
        if (user != null) {

            HomeUser homeUser = homeUserService.getOneById(user.getId());
            System.out.println("@ModelAttribute  getUser id------" + user.getId()+"    "+homeUser);
            homeUser.setUserAddresses(null);
            homeUser.setProjects(null);
            model.addAttribute("homeUser", homeUser);
        }

    }

    /**
     * 用户自己退出
     *
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("logout")
    public void logout(HttpServletResponse response, HttpSession session) throws IOException {
        session.setAttribute("home_user", null);
        response.sendRedirect("/toLogin");
    }

    /**
     * xiu修改密码
     * @param
     * @param newPassword
     * @param rePassword
     * @param session
     * @return
     */
    @RequestMapping("changePassword")
    public Map<String, Object> changePassword(String oldpassword, String newPassword,String rePassword, HomeUser user,HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        if (session.getAttribute("home_user") != null) {
            if (rePassword.contains(newPassword)){

                boolean b = homeUserService.changePassword(user,oldpassword, newPassword, rePassword);

                if (b){
                    map.put("msg", "修改成功");
                    map.put("code", 0);
                }else {
                    map.put("msg", "原密码不正确");
                    map.put("code", 3);
                }

            }else {
                map.put("msg", "两次密码不一致");
                map.put("code", 2);
            }

        } else {
            map.put("msg", "更新失败，没有登录");
            map.put("code", 1);
        }
        return map;

    }
}
